package com.wildlife.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wildlife.springboot.common.Result;
import com.wildlife.springboot.entity.Info;
import com.wildlife.springboot.service.IInfoService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/info")
public class InfoController {

    @Resource
    private IInfoService infoService;

    //新增和修改
    @PostMapping
    public Result save(@RequestBody Info info){
        //新增或更新
        return Result.success(infoService.saveOrUpdate(info));
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(infoService.removeById(id));
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        return Result.success(infoService.removeByIds(ids));
    }

    @GetMapping
    public Result findAll() {
        return Result.success(infoService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(infoService.getById(id));
    }

    @GetMapping("/name/{name}")
    public Result findOne(@PathVariable String name) {
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        return Result.success(infoService.getOne(queryWrapper));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String name
    ) {
        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
//        if (!"".equals(email)) {
//            queryWrapper.like("email", email);
//        }
//        if (!"".equals(address)) {
//            queryWrapper.like("address", address);
//        }
        return Result.success(infoService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


//    @GetMapping("/selectAll")
//    public Result selectAll(@RequestBody Info info) {
//        List<Info> list = infoService.selectAll();
//        return Result.success(list);
//    }

}
